package com.mz.mattress.admin.service.impl;

import com.mz.mattress.admin.mapper.MerchantGradeMapper;
import com.mz.mattress.admin.service.MerchantGradeService;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.MerchantGrade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

@Service
@Slf4j
public class MerchantGradeServiceImpl implements MerchantGradeService {

    @Autowired
    private MerchantGradeMapper merchantGradeMapper;

    @Override
    public Result<List<MerchantGrade>> list() {
        List<MerchantGrade> list = merchantGradeMapper.list();
        return Result.ok(list);
    }

    @Override
    public Result<MerchantGrade> getById(Serializable id) {
        MerchantGrade merchantGrade = merchantGradeMapper.selectById(id);
        return Result.ok(merchantGrade);
    }

    @Override
    public Result updateById(MerchantGrade merchantGrade) {

        merchantGrade.setName(null);
        merchantGrade.setLevel(null);

        Integer bonusRatio = merchantGrade.getBonusRatio();

        if (bonusRatio == null) {
            return Result.fail("请输入佣金比例");
        }

        if (bonusRatio > 100 || bonusRatio < 1) {
//            return Result.fail("请输入合理佣金比例");
            return Result.fail("佣金比例范围（1-100）");
        }

        merchantGradeMapper.updateById(merchantGrade);

        return Result.ok();
    }

}
